// pages/mine/myform/applyReturnGoods/applyReturnGoods.js
const app = getApp();
const util = require("../../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    returnList: [
      {
        id: 0,
        isselect: true,
        text: "七天无理由退货"
      },
      {
        id: 1,
        isselect: false,
        text: "质量问题"
      },
      {
        id: 2,
        isselect: false,
        text: "物流太慢"
      },
      {
        id: 3,
        isselect: false,
        text: "其他原因"
      }
    ],
    isunfold: true,
    orderId: "",
    order: [],
    reason: "七天无理由退货",
    detailReason: "",
    img: [],
    files: [],
    pics: []
  },

  selectReturn: function (e) {
    this.data.isunfold = !this.data.isunfold
    this.setData(this.data)
  },

  // 选择原因
  selectTrouble: function (e) {
    let that = this
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    for (let i = 0; i < that.data.returnList.length; i++) {
      if (index == i) {
        that.data.returnList[i].isselect = true
        // console.log(that.data.returnList[i].text)
        that.setData({
          reason: that.data.returnList[i].text
        })
      } else {
        that.data.returnList[i].isselect = false
      }
      this.setData(this.data)
    }
  },

  // 详细原因
  detailReason: function (e) {
    // console.log(e)
    this.setData({
      detailReason: e.detail.value
    })
  },

  //多张图片上传
  uploadimg: function (data) {
    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0; //上传失败的个数
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'file', //这里根据自己的实际情况改
      formData: data.data, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1
        wx.navigateBack({
          delta: 1
        })
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
        wx.navigateBack({
          delta: 1
        })
      },
      complete: () => {
        // console.log(i);
        i++; //这个图片执行完上传后，开始上传下一张
        if (i == data.path.length) { //当图片传完时，停止调用          
          // console.log('执行完毕');
          // console.log('成功：' + success + " 失败：" + fail);
        } else { //若图片还没有传完，则继续调用函数
          // console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    });
  },

  // 上传图片
  chooseImage: function (e) {
    var that = this,
      pics = that.data.pics
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        // console.log(res)
        that.setData({
          pics: res.tempFilePaths
        })
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
        // console.log(that.data.files)
        // let img = {
        //   paramName: "上传图片",
        //   option: that.data.files
        // }
        // that.data.img.push(img)
        that.setData(that.data)
        // console.log(that.data.img)
        let imgsrc = res.tempFilePaths
        pics = pics.concat(imgsrc)
        // that.setData({
        //   pics: pics
        // })
      }
    })
  },

  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

  // 提交申请
  submitApply: function (e) {
    let that = this
    // 发送换货申请
    let returnOrderUrl = app.globalData.baseUrl + "weixin/returnOrder"
    let data = {
      orderId: that.data.orderId,
      reason: that.data.reason,
      detailReason: that.data.detailReason,
      // pictureLocs: []
    }
    util.requestLoading(returnOrderUrl, data, "GET").then(res => {
      // console.log(res)
      // 上传图片
      let pics = that.data.pics
      // console.log(pics)
      if (pics.length == 0) {
        wx.navigateBack({
          delta: 1
        })
      } else {
        that.uploadimg({
          url: app.globalData.baseUrl + 'weixin/changeOrderPic',
          path: pics,
          data: {
            orderId: that.data.orderId
          }
        })
      }
      if (res.data.status == 0) {
        wx.hideLoading()
        wx.reLaunch({
          url: '../myform',
        })
        // that.setData({
        //   order: res.data.order
        // })
      } else {
        wx.hideLoading()
        wx.showModal({
          title: '提交失败',
          content: '订单异常，请在订单列表中查看',
          success(res) {
            if (res.confirm) {
              wx.reLaunch({
                url: '../myform',
              })
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      orderId: options.orderId
    })
    // console.log(that.data.orderId)
    // 请求页面数据
    let changeOrderDetailUrl = app.globalData.baseUrl + "weixin/changeOrderDetail"
    let data = {
      orderId: that.data.orderId
    }
    util.requestLoading(changeOrderDetailUrl, data, "GET").then(res => {
      // console.log(res)
      that.setData({
        order: res.data.order
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})